<template>
  <div class="container">
    <div class="fixed-box">
      <div class="search-bar">
        <div class="search-box">
          <input
            type="text"
            @confirm="onSearch"
            v-model="searchVal"
            style="
              height: 60rpx;
              border-radius: 50rpx;
              border: 0;
              margin: 0 30rpx 0 64rpx;
              line-height: 48rpx;
              vertical-align: top;
              background: #f7f7f7;
              font-size: 28rpx;
            "
          />
          <img
            src="../../../static/images/icon/search.png"
            alt=""
            style="
              width: 32rpx;
              height: 32rpx;
              position: absolute;
              left: 20rpx;
              top: 14rpx;
              display: block;
            "
          />
        </div>
        <div
          @click="pageLoad"
          style="width: 40rpx; height: 40rpx; font-size: 0"
        >
          <img
            :src="
              flag
                ? '../../../static/images/icon/search-col2.png'
                : '../../../static/images/icon/search-col.png'
            "
            alt=""
            style="width: 100%; height: 100%"
          />
        </div>
      </div>
      <div class="tabs">
        <span
          v-for="(item, index) in sort"
          :key="index"
          @click="tab(index)"
          :class="index == ind ? 'tab-item active' : 'tab-item'"
          >{{ item }}</span
        >
      </div>
    </div>
    <div class="prod-list">
      <div :class="flag ? 'cont-item' : 'prod-show'">
        <div
          :class="flag ? 'show-item' : 'prod-items'"
          v-for="item in searchProdPage"
          :key="item.prodId"
          @click="goDetail(item.prodId)"
        >
          <div :class="flag ? 'more-prod-pic' : 'hot-imagecont'">
            <img
              :class="flag ? 'more-pic' : 'hotsaleimg'"
              :src="item.pic"
              alt=""
            />
          </div>
          <div :class="flag ? 'prod-text-right' : 'hot-text'">
            <div :class="flag ? 'prod-text' : 'hotprod-text'">
              {{ item.prodName }}
            </div>
            <div :class="flag ? 'cate-prod-info' : 'prod-info'">
              {{ item.praiseNumber }}评价&nbsp;{{ item.prodCommNumber }}好评
            </div>
            <div :class="flag ? 'prod-price' : 'prod-text-info'">
              <span class="symbol">￥</span>
              <span class="big-num">{{ item.price }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getSearchProdPage } from "@/services/index";
export default {
  data() {
    return {
      searchVal: "",
      ind: 0,
      flag: true,
      sort: ["综合", "销量", "价格"],
      parameter: {
        current: 1,
        prodName: this.searchVal,
        size: 10,
        sort: 0,
      },
      searchProdPage: [],
    };
  },
  onLoad: function (option) {
    this.searchVal = option.val;
    this.parameter.prodName = option.val;
  },
  watch: {
    searchVal(newVal) {
      this.parameter.prodName = newVal;
    },
    async ind(newVal) {
      this.parameter.sort = newVal;
      let res = await getSearchProdPage(this.parameter);
      this.searchProdPage = res.records;
    },
  },
  async created() {
    let res = await getSearchProdPage(this.parameter);
    this.searchProdPage = res.records;
  },
  methods: {
    tab(i) {
      this.ind = i;
    },
    async onSearch() {
      let res = await getSearchProdPage(this.parameter);
      this.searchProdPage = res.records;
    },
    pageLoad() {
      this.flag = !this.flag;
    },
    goDetail(prodId) {
      wx.navigateTo({
        url: `../../category/prod/detail?prodId=${prodId}`,
      });
    },
  },
};
</script>

<style scoped>
.container {
  height: 100%;
  box-sizing: border-box;
  color: #333;
  font-family: helvetica, "Heiti SC", PingFangSC-Light;
}
.fixed-box {
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 999;
  background: #fff;
}
.search-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  color: #777;
  background: #fff;
  z-index: 3;
  padding: 0 30rpx;
  box-sizing: border-box;
}
.search-box {
  position: relative;
  height: 60rpx;
  background: #f7f7f7;
  z-index: 999;
  width: 80%;
  border-radius: 50rpx;
  margin-right: 30rpx;
  flex: 1;
}
.tabs {
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
  padding: 10rpx 0;
  z-index: 999;
  background: #fff;
}
.tab-item {
  display: inline-block;
  width: 33.33%;
  text-align: center;
  font-size: 28rpx;
}
.active {
  color: #eb2444;
}
.cont-item {
  padding: 0 20rpx 20rpx 20rpx;
  margin-top: 180rpx;
}
.prod-show {
  background: #fff;
  margin-top: 160rpx;
}
.show-item {
  position: relative;
  display: flex;
  justify-content: start;
  padding: 20rpx;
  border-radius: 20rpx;
  background: #fff;
  margin-bottom: 20rpx;
  box-shadow: 0 16rpx 32rpx 0 rgba(7, 17, 27, 0.05);
}
.more-prod-pic {
  text-align: center;
  width: 170rpx;
  height: 170rpx;
  font-size: 0;
}
.more-pic {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}
.prod-text-right {
  margin-left: 20rpx;
  width: 75%;
}
.prod-text {
  margin: 0;
  height: 78rpx;
  font-size: 28rpx;
  word-break: break-all;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  color: #000;
}
.cate-prod-info {
  font-size: 22rpx;
  color: #999;
  margin: 10rpx 0 20rpx 0;
}
.prod-price {
  font-size: 28rpx;
  color: #eb2444;
  font-family: arial;
}
.prod-items {
  width: 375rpx;
  float: left;
  background: #fff;
  padding-bottom: 20rpx;
  box-sizing: border-box;
  padding: 20rpx 10rpx 10rpx 20rpx;
}
.hot-imagecont {
  border-radius: 8rpx;
  text-align: center;
  font-size: 0;
}
.hot-text {
  margin-top: 20rpx;
}
.hotsaleimg {
  width: 345rpx;
  height: 345rpx;
}
.hotprod-text {
  height: 76rpx;
  font-size: 28rpx;
  word-break: break-all;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  color: #000;
}
.prod-info {
  font-size: 20rpx;
  color: #777;
  margin-top: 8rpx;
}
.prod-text-info {
  position: relative;
  height: 50rpx;
  line-height: 70rpx;
  font-family: Arial;
}
.price {
  font-family: Arial;
  display: inline-block;
  padding-bottom: 10rpx;
  padding-left: 10rpx;
}
.symbol {
  font-size: 24rpx;
  color: #eb2444;
}
.big-num {
  font-size: 32rpx;
  color: #eb2444;
}
</style>